package com.crane.oss.web.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crane.common.core.dto.*;
import com.crane.oss.sys.bean.request.user.*;
import com.crane.oss.sys.bean.resp.UserBean;
import com.crane.oss.enums.UserTypeEnum;
import com.crane.oss.properties.SysConfigProperties;
import com.crane.oss.sys.service.UserService;
import com.crane.oss.web.BaseBusinessController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * @author bealon
 * @email ccsu123456@qq.com
 * @date 2018年12月28日 12时22分23秒
 */
@SuppressWarnings("rawtypes")
@Api(value = "userController", tags = {"系统管理-用户管理"})
@RestController
@RequestMapping("/sys/user")
public class UserController extends BaseBusinessController {

    @Autowired
    private UserService userService;

    @Autowired
    SysConfigProperties sysConfigProperties;

    @ApiOperation(value = "获取用户列表信息", notes = "获取用户列表信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryUserRequest")
    @RequiresPermissions("sys:user:list")
    @PostMapping("/list")
    public MultiResponse list(@RequestBody QueryUserRequest request) {
        SessUser user = getUser();
        if (!user.getUserType().equals(UserTypeEnum.SUPER_ADMIN.getType())
                && !user.getUserType().equals(UserTypeEnum.SYSTEM_ADMIN.getType())) {
            request.setUserType(UserTypeEnum.COMMON_USER.getType());
        }
        if(null == request.getPartyId()){
            request.setPartyId(getPartyId());
        }
        request.setOpPartyId(getPartyId());
        IPage<UserBean> page = userService.queryList(request);
        return MultiResponse.of(page);
    }

    @ApiOperation(value = "获取用户详细信息", notes = "获取用户详细信息")
    @RequiresPermissions("sys:user:info")
    @PostMapping("/info")
    public SingleResponse info(@RequestBody String data) {
        String sid = getParamter(data, "id");
        UserBean userBean = userService.queryUser(getPartyId(), Long.parseLong(sid));
        return SingleResponse.of(userBean);
    }


    @ApiOperation(value = "添加其他机构用户", notes = "添加其他机构用户")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddUserPartyIdRequest")
    @RequiresPermissions("sys:user:save") // @SysLog("保存")
    @PostMapping("/addUser")
    public Response addUser(@RequestBody @Valid AddUserPartyIdRequest request) {
        if(null == request.getPartyId()){
            request.setPartyId(getPartyId());
        }
        userService.addUser(request.getUserName(),request.getPartyId());
        return Response.buildSuccess();
    }

    @ApiOperation(value = "新增用户", notes = "新增用户信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddUserRequest")
    @RequiresPermissions("sys:user:save") // @SysLog("保存")
    @PostMapping("/save")
    public Response save(@RequestBody @Valid AddUserRequest request) {
        request.setCreateUserId(getUserId());
        request.setCreatePartyId(getPartyId());
        userService.save(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改用户", notes = "修改用户")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdateUserRequest")
    @RequiresPermissions("sys:user:update") // @SysLog("修改")
    @PostMapping("/update")
    public Response update(@RequestBody @Valid UpdateUserRequest request) {
        request.setUpdateUserId(getUserId());
        request.setUpdatePartyId(getPartyId());
        userService.update(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @RequiresPermissions("sys:user:delete") // @SysLog(value="删除",self=true)
    @PostMapping("/delete")
    public Response delete(@RequestBody String data) {
        String sid = getParamter(data, "id");
        userService.delete(Long.parseLong(sid),getPartyId());
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改用户登录密码", notes = "修改用户登录密码")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdateUserPassWordRequest")
    @RequiresPermissions("sys:user:updatePassword") // @SysLog(value="批量删除",self=true)
    @PostMapping("/updatePassword")
    public Response updatePassword(@RequestBody @Valid UpdateUserPassWordRequest request) {
        userService.updatePassword(getUser().getUserName(), request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改用户状态", notes = "修改用户状态")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdateUserStateRequest")
    @RequiresPermissions("sys:user:update") // @SysLog(value="批量删除",self=true)
    @PostMapping("/updateState")
    public Response updateState(@RequestBody @Valid UpdateUserStateRequest request) {
        request.setUpdateUserId(getUserId());
        request.setUpdatePartyId(getPartyId());
        userService.updateState(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "获取用类型列表信息", notes = "获取用类型列表信息")
    @GetMapping("/userType")
    public MultiResponse getUserTypeList() {
        // 查询列表数据
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserTypeEnum userTypeEnum : UserTypeEnum.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("key", userTypeEnum.getType());
            map.put("name", userTypeEnum.getRemark());
            list.add(map);
        }
        return MultiResponse.ofWithoutTotal(list);
    }

    @ApiOperation(value = "根据用户名生成账号", notes = "根据用户名生成账号")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryUserNameRequest")
    @RequiresPermissions("sys:user:save") // @SysLog("保存")
    @PostMapping("/getUserName")
    public SingleResponse getUserName(@RequestBody @Valid QueryUserNameRequest request) {
        return SingleResponse.of(userService.getUserName(request.getUserName()));
    }

}
